package com.pp.controller;

import com.pp.entity.User;
import com.pp.util.UserUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanou on 18/1/16.
 */
@WebServlet("/select")
public class SelectController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("userName");
        String pwd = request.getParameter("userPwd");

        String step = request.getParameter("step");
        String page = request.getParameter("page");

        String pageA = request.getParameter("pageA");
        int size = 3;
        //page为空时，是第一次打开程序
        if (null == page || "".equals(page)) {
            page = "1";

        }
        int temp = 0;

        if (step != null) {
            if (step.equals("up")) {

                temp = Integer.parseInt(page) - 1;

            } else {
                temp = Integer.parseInt(page) + 1;
            }
        } else {
            temp = 1;
        }
        List list = new ArrayList<>();
        if (pageA != null || !"".equals(pageA)) {
            try {
                temp=Integer.parseInt(pageA);

            } catch (Exception e) {

            }
        }else {
            temp=temp;
        }

        String sql = null;
        String sql2 = null;
        int page2 = (temp - 1) * 3;
        if ((null == name || "".equals(name)) && (null == pwd || "".equals(pwd))) {
            sql = "SELECT * FROM login_users limit " + page2 + ",3";
            sql2 = "SELECT COUNT(*) c FROM login_users ";
        } else if (null == name || "".equals(name)) {
            sql = "SELECT * FROM login_users WHERE pwd='" + pwd + "' limit " + page2 + ",3";
            sql2 = "SELECT COUNT(*) c FROM login_users WHERE pwd='" + pwd + "'";
        } else if (null == pwd || "".equals(pwd)) {
            sql = "SELECT * FROM login_users WHERE name='" + name + "' limit " + page2 + ",3";
            sql2 = "SELECT COUNT(*) c FROM login_users WHERE name='" + name + "'";
        } else {
            sql = "SELECT * FROM login_users WHERE name='" + name + "' AND pwd = '" + pwd + "' limit " + page2 + ",3";
            sql2 = "SELECT COUNT(*) c FROM login_users WHERE name='" + name + "' AND pwd = '" + pwd + "'";
        }
        UserUtil uu = new UserUtil();
        Statement stt = uu.JDBCUtil();
        Statement countStt = uu.JDBCUtil();
        List<User> ulist = new ArrayList<>();
        try {
            ResultSet rs = stt.executeQuery(sql);
            ResultSet countRs = countStt.executeQuery(sql2);
            countRs.next();
            int count = countRs.getInt("c");
            int p = count / size;
            int y = count % size;
            if (y > 0) {
                p += 1;
            }
            while (rs.next()) {
                User user = new User(rs.getInt("id"), rs.getString("name"), rs.getString("pwd"), rs.getString("phone"));
                ulist.add(user);
            }


            if((p-temp)<3){
                if(temp<p-1) {
                    for (int i = temp - 1; i < (p - temp + 1) + temp - 1; i++) {
                        list.add(i);
                    }
                }else{
                    for(int i = temp;i<(p - temp + 1) + temp;i++){
                        list.add(i);
                    }
                }
            }else {
                if(temp>1) {
                    for (int i = temp - 1; i < (3 + temp) - 1; i++) {
                        list.add(i);
                    }
                }else{
                    for(int i = temp;i<3+temp;i++){
                        list.add(i);
                    }
                }
            }

            request.setAttribute("num",list);
            request.setAttribute("userName",name);
            request.setAttribute("userPwd",pwd);
            request.setAttribute("countP", p);
            request.setAttribute("users", ulist);
            request.setAttribute("page", temp);
            if (temp < p) {
                request.setAttribute("down", "down");
            }
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
